M98 P"0:/sys/led/resetstatus.g"

M568 P0 R0 S0
M568 P1 R0 S0
M568 P2 R0 S0
M568 P3 R0 S0

; ========================== T0 CONNECTION ==========================

M291 R"Left Tool (T0) Test" P"This test will verify Left Tool (T0) connections:<br><br>- Part Cooling Fan<br>- Hotend Fan<br>- Heater<br>- Thermistor<br>- Extruder Motor" S4 K{"Start Test","Skip","Cancel"}
if input == 0
  
  ; ///// T0 Fans Test /////
  M106 P3 S0
  M106 P2 H0 S1 B0 T300
  M291 R"T0 Fans Test" P"Confirm that both Tool 0 fans are NOT spinning." S4 K{"T0 Fans OFF","Cancel"}
  if input == 1
    abort "Test cancelled by user"

  M106 P3 S1
  M291 R"T0 Part Cooling Fan Test" P"Confirm that the Tool 0 Part Cooling Fan is spinning." S4 K{"T0 PCF Spinning","Cancel"}
  if input == 1
    abort "Test cancelled by user"

  M471 S"0:/sys/daemon.g" T"0:/sys/daemon.g.off"                  ; OFF daemon.g

  M106 P3 S0
  M106 P2 H1 S1 B0 T0
  M291 R"T0 Hotend Fan Test" P"Confirm that the Tool 0 Hotend Fan is spinning." S4 K{"T0 Hotend Fan ON","Cancel"}

  if input == 1
    M106 P3 S0
    M106 P2 H0 T70 S1 B0
    M471 S"0:/sys/daemon.g.off" T"0:/sys/daemon.g"                  ; ON daemon.g
    abort "Test cancelled by user"
  
  M106 P3 S0
  M106 P2 H0 T70 S1 B0

  M471 S"0:/sys/daemon.g.off" T"0:/sys/daemon.g"                  ; ON daemon.g
  ;// ///// T0 Fans Test End /////


  ; ///// T0 Heat Test /////
  T0 P0

  var T0_Min_Temp = 60                                                                            ; Minimum temperature to start the test
  var T0_Raise_Temp = 30                                                                          ; Raise the temperature
  var T0_Max_Heat_Time = 60                                                                       ; Maximum time to heat up the T0 Heater

  M291 R"T0 Heater Test" P"Place your hand near Tool 0 heater block to feel warmth during heating test." S4 K{"Start Test","Skip","Cancel"}
  if input == 2
    abort "Test cancelled by user"

  if input == 0
    ; Check if T0 is cold enough
    if heat.heaters[0].current > var.T0_Min_Temp
      M98 P"0:/sys/led/fault.g"
      M291 R"T0 Heater Test" P"Tool 0 heater is currently above safe testing temperature. Waiting for cooldown..." S1 T0
      while heat.heaters[0].current > var.T0_Min_Temp
        G4 S1
        if sensors.endstops[0].triggered || sensors.endstops[3].triggered
          break
      M98 P"0:/sys/led/resetstatus.g"
      M292
    var T0_Start_Temp = heat.heaters[0].current                                                     ; Save the current T0 temperature
    var T0_Start_Time = state.time                                                                  ; Save the current time
    var T0_Target_Temp = var.T0_Start_Temp + var.T0_Raise_Temp                                      ; Calculate the target temperature

    M568 P0 R200 S200                                              ; Set the T0 temperature to the target temperature
    M98 P"0:/sys/led/start_cold.g"                                                                  ; LED Signalize preheating

    while heat.heaters[0].current < var.T0_Target_Temp
      G4 P100
      if sensors.endstops[0].triggered || sensors.endstops[3].triggered
        break

    M568 P0 R0 S0                                                                                    ; Turn off the T0 heater
    M98 P"0:/sys/led/end.g"                                                                         ; LED Signalize the end of the test

    var T0_Heat_Time = state.time - var.T0_Start_Time                                               ; Calculate the time it took to heat up T0
    echo "T0 Heater took " ^ var.T0_Heat_Time ^ " seconds to heat up"
    if var.T0_Heat_Time > var.T0_Max_Heat_Time
      M98 P"0:/sys/led/fault.g"
      M291 R"T0 Heater Test" P"T0 Heater took too long to heat up" S1 T200
      abort "Error: T0 Heater took too long to heat up"
    M98 P"0:/sys/led/resetstatus.g"

    ; Touch Verification - Confirm user felt heat
    M291 R"T0 Heat Verification" P"Did you feel heat from the Left Tool (Tool 0) heater block?" S4 K{"Left Tool is Hot","Left Tool NOT Hot","Cancel"}
    if input == 1
      M98 P"0:/sys/led/fault.g"
      abort "Error: Left Tool (Tool 0) did not heat properly - user did not feel heat"
    elif input == 2
      abort "Test cancelled by user"
  ; ///// T0 Heater Test End /////

  ; ///// T0 Extruder Motor Direction Test /////
  while true
    M291 R"Left Tool (T0) Extruder Motor Direction Test" P"Ensure filament is completely removed from Left Tool extruder.<br><br>Gears should spin UPWARDS ↑ during test." S4 K{"Filament Removed - Start","Skip Test","Cancel"}
    if input == 0
      M302 P1 ; Allow cold extrudes
      T0 P0
      M83 ; Extruder to relative mode
      G1 E100 F1000 ; Feed filament
      M400
      M302 P0 ; Disallow cold extrudes
    elif input == 1
      break
    elif input == 2
      abort "Test cancelled by user"
    if sensors.filamentMonitors[1].status != "ok"
      break

; ///// T0 Extruder Motor Direction Test End /////











; ========================== T1 CONNECTION ==========================

M291 R"Right Tool (T1) Test" P"This test will verify Right Tool (T1) connections:<br><br>- Part Cooling Fan<br>- Hotend Fan<br>- Heater<br>- Thermistor<br>- Extruder Motor" S4 K{"Start Test","Skip","Cancel"}
if input == 0

  ; ///// T1 Fans Test /////
  M106 P1 S0
  M106 P0 H1 S1 B0 T300
  M291 R"T1 Fans Test" P"Confirm that both Tool 1 fans are NOT spinning." S4 K{"T1 Fans OFF","Cancel"}
  if input == 1
    abort "Test cancelled by user"

  M106 P1 S1
  M291 R"T1 Part Cooling Fan Test" P"Confirm that the Tool 1 Part Cooling Fan is spinning." S4 K{"T1 PCF Spinning","Cancel"}
  if input == 1
    abort "Test cancelled by user"


  M471 S"0:/sys/daemon.g" T"0:/sys/daemon.g.off"                  ; OFF daemon.g

  M106 P1 S0
  M106 P0 H1 S1 B0 T0
  M291 R"T1 Hotend Fan Test" P"Confirm that the Tool 1 Hotend Fan is spinning." S4 K{"T1 Hotend Fan ON","Cancel"}

  if input == 1
    M106 P1 S0
    M106 P0 H1 T70 S1 B0
    M471 S"0:/sys/daemon.g.off" T"0:/sys/daemon.g"                  ; ON daemon.g
    abort "Test cancelled by user"

  M106 P1 S0
  M106 P0 H1 T70 S1 B0

  M471 S"0:/sys/daemon.g.off" T"0:/sys/daemon.g"                  ; ON daemon.g
  ; ///// T1 Fans Test End /////


  ; ///// T1 Heat Test /////
  T1 P0

  var T1_Min_Temp = 60                                                                          ; Minimum temperature to start the test
  var T1_Raise_Temp = 30                                                                        ; Temperature increase
  var T1_Max_Heat_Time = 60                                                                     ; Maximum heating time

  M291 R"T1 Heater Test" P"Place your hand near Tool 1 heater block to feel warmth during heating test." S4 K{"Start Test","Skip","Cancel"}
  if input == 2
    abort "Test cancelled by user"

  if input == 0
    ; Check if T1 is cold enough
    if heat.heaters[1].current > var.T1_Min_Temp
      M98 P"0:/sys/led/fault.g"
      M291 R"T1 Heater Test" P"Tool 1 heater is currently above safe testing temperature. Waiting for cooldown..." S1 T0
      while heat.heaters[1].current > var.T1_Min_Temp
        G4 S1
        if sensors.endstops[0].triggered || sensors.endstops[3].triggered
          break
      M98 P"0:/sys/led/resetstatus.g"
      M292

    var T1_Start_Temp = heat.heaters[1].current                                                   ; Save current T1 temperature
    var T1_Start_Time = state.time                                                                ; Save current time
    var T1_Target_Temp = var.T1_Start_Temp + var.T1_Raise_Temp                                    ; Calculate target temperature

    M568 P1 R200 S200                                            ; Set T1 temperature
    M98 P"0:/sys/led/start_cold.g"                                                                ; LED Signalize preheating

    while heat.heaters[1].current < var.T1_Target_Temp
      G4 P100
      if sensors.endstops[0].triggered || sensors.endstops[3].triggered
        break

    M568 P1 R0 S0                                                                                  ; Turn off T1 heater
    M98 P"0:/sys/led/end.g"                                                                       ; LED Signalize end of test

    var T1_Heat_Time = state.time - var.T1_Start_Time                                             ; Calculate heating time
    echo "T1 Heater took " ^ var.T1_Heat_Time ^ " seconds to heat up"
    if var.T1_Heat_Time > var.T1_Max_Heat_Time
      M98 P"0:/sys/led/fault.g"
      M291 R"T1 Heater Test" P"T1 Heater took too long to heat up" S1 T200
      abort "Error: T1 Heater took too long to heat up"
    M98 P"0:/sys/led/resetstatus.g"

    ; Touch Verification - Confirm user felt heat
    M291 R"T1 Heat Verification" P"Did you feel heat from the Right Tool (Tool 1) heater block?" S4 K{"Right Tool is Hot","Right Tool NOT Hot","Cancel"}
    if input == 1
      M98 P"0:/sys/led/fault.g"
      abort "Error: Right Tool (Tool 1) did not heat properly - user did not feel heat"
    elif input == 2
      abort "Test cancelled by user"
    ; ///// T1 Heater Test End /////

    ; ///// T1 Extruder Motor Direction Test /////
  while true
    M291 R"Right Tool (T1) Extruder Motor Direction Test" P"Ensure filament is completely removed from Right Tool extruder.<br><br>Gears should spin UPWARDS ↑ during test." S4 K{"Filament Removed - Start","Skip Test","Cancel"}
    if input == 0
      M302 P1 ; Allow cold extrudes
      T1 P0
      M83 ; Extruder to relative mode
      G1 E100 F1000 ; Feed filament
      M400
      M302 P0 ; Disallow cold extrudes
    elif input == 1
      break
    elif input == 2
      abort "Test cancelled by user"
    if sensors.filamentMonitors[0].status != "ok"
      break

  T0 P0 
; ///// T1 Extruder Motor Direction Test End /////



T0 P0
M98 P"0:/sys/led/resetstatus.g"